package com.junxonline.plat.dao.repository;

import com.junxonline.plat.dao.model.Position;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: PositionRepository
 * @Description: 部门岗位数据操作类
 * @author JunX
 */

@Repository
public interface PositionRepository extends JpaRepository<Position, Long>, JpaSpecificationExecutor<Position> {

    Position findByPositionId(Long positionId);

    List<Position> findByPositionIdIn(Long[] positionIds);

    @Query(value = "SELECT user_position.* FROM sys_user_position user_position WHERE user_position.position_id = ?1", nativeQuery = true)
    List<Map<String, BigInteger>> findUserPositionsByPositionId(Long roleId);

    List<Position> findPositionsByPositionTypeIn(Integer[] positionType, Sort sort);

    @Modifying
    @Query(value ="DELETE FROM sys_position_menu WHERE menu_id = ?1 AND position_id = ?2", nativeQuery = true)
    void deletePositionMenuIn(Long menuId, Long positionId);

    @Modifying
    @Query(value ="DELETE FROM sys_user_position WHERE user_id = ?1 AND position_id = ?2", nativeQuery = true)
    void deleteUserPositionIn(Long userId, Long positionId);

    @Modifying
    @Query(value = "INSERT INTO sys_position_menu (menu_id, position_id) VALUES(?1, ?2)", nativeQuery = true)
    void savePositionMenuIn(Long menuId, Long positionId);

}
